<?php
//
namespace app\home\controller;

use app\common\model\SpecialModel;
use Org\Util\AliSms;
use think\Request;

class IndexController extends BaseController
{

    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    /*
     * 网站首页
     */
    public function index()
    {
        return $this->fetch();
    }

    /*
     * 专题页
     */
    public function special()
    {
        $specialId    = $this->request->param('special_id/d');
        $specialModel = new SpecialModel();
        $specialInfo  = $specialModel->find($specialId);
        //dump($specialInfo);
        if ($specialInfo && $specialInfo['special_status'] == 1) {
            $this->assign('keyword',$specialInfo['special_keyword']);
            $this->assign('description',$specialInfo['special_description']);
            $this->assign('specialInfo', $specialInfo);
            return $this->display($specialInfo['special_content']);
        } else {
            $this->error('请先发布该专题！');
        }
    }

    /**
     * 店铺定位
     * @return mixed
     */
    public function pos()
    {
        $url = $this->request->param('url', 0);
        if ($url) {
            $url = urldecode($url);
        } else {
            $url = url('/home/Index/index');
        }
        if ($this->request->isPost()) {
            \cookie('lng', $this->request->param('lng', 0),3600);
            \cookie('lat', $this->request->param('lat', 0),3600);
            \cookie('area', $this->request->param('area', '中国'),3600);
            $this->success('定位成功', $url);
        }

        $html = "
            <style>
            *{padding: 0;margin: 0}
            .img {
            position: absolute;
            top: 50vh;
            margin-top: -37.5vh;
            width: 100vw;height: auto;}
            </style>
            <div style='height: 100vh'>
            <img src='/public/home/img/pos.gif' alt='' class='img'>
            </div>
            <script src='/public/plugins/jquery.min.js'></script>
            <script src=\"/public/plugins/jquery.cookie.js\"></script>
            <script type=\"text/javascript\" src=\"http://api.map.baidu.com/api?v=2.0&ak=iLd3FbCLOXfHKoIBWNANyBtNGc8mjS8k\"></script>
            <script >
                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(function(r){
                    if(this.getStatus() == BMAP_STATUS_SUCCESS){
                        var address=r.address;
                        var data={};
                        data['url']='" . $url . "';
                        if (address.district){
                            data['area']=address.district;
                        }else {
                            data['area']=address.city;
                        }
                            data['lng']=r.point.lng;
                            data['lat']=r.point.lat;
                        $.post('" . get_current_url() . "',data,function(data) {
                            location.href=data.url;
                        })
                    }
                    else {
                        alert('failed'+this.getStatus());
                    }
                },{enableHighAccuracy: true});
            </script>
        ";

        return $this->display($html);
    }
	
}
